package com.scott.crm.teacher.controller;
import com.scott.base.BaseBean;
import com.scott.base.message.DataRes;
import com.scott.base.message.DataUtil;
import com.scott.base.utils.AESUtil;
import com.scott.crm.clazz.bean.ClazzBean;
import com.scott.crm.clazz.bean.ClazzStudentTeacherBean;
import com.scott.crm.clazz.service.ClazzService;
import com.scott.crm.clazz.service.ClazzStudentTeacherService;
import com.scott.crm.delay.bean.ClazzUserBean;
import com.scott.crm.delay.bean.DelayBean;
import com.scott.crm.role.service.RoleService;
import com.scott.crm.teacher.bean.TeacherBean;
import com.scott.crm.teacher.dto.MyDto;
import com.scott.crm.teacher.service.TeacherService;
import com.scott.crm.user.bean.UserBean;
import com.scott.crm.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private UserService userService;
    @Autowired
    private ClazzService clazzService;
    @Autowired
    private ClazzStudentTeacherService clazzStudentTeacherService;
    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/show/clazz",method = RequestMethod.GET)
    public DataRes showClazz() throws Exception{


        List<ClazzBean> allClazz = clazzService.selectAllClazz();


        if(!CollectionUtils.isEmpty(allClazz)){
            return DataUtil.success(allClazz);
        }else{
            return DataUtil.errors();
        }
    }

    @RequestMapping(value = "/assign/clazz",method = RequestMethod.POST)
    public DataRes assignClazz(@RequestBody ClazzStudentTeacherBean clazzStudentTeacherBean) throws Exception{


        Integer addTeacher = clazzStudentTeacherService.addTeacher(clazzStudentTeacherBean);


        if(addTeacher!=0){
            return DataUtil.success(addTeacher);
        }else{
            return DataUtil.errors();
        }
    }

    @RequestMapping(value = "delete/clazz",method = RequestMethod.POST)
    public DataRes deleteAssignClazz(@RequestBody Map<String,List<Integer>> map) throws Exception{


        if(CollectionUtils.isEmpty(map)||map.get("ids").isEmpty()){
            return DataUtil.errors();
        }

        List<Integer> ids = map.get("ids");


        int counter = teacherService.deleteAssignClazz(ids);
        if(counter >0){
            return DataUtil.success(counter);
        }else{
            return DataUtil.errors();
        }
    }

    @RequestMapping(value = "/updateClazz",method = RequestMethod.POST)
    public  DataRes updateClazz(@RequestBody MyDto myDto)throws Exception{

        teacherService.updateClazz(myDto);
        int counter=teacherService.updateClazz(myDto);
        if(counter==1){
            return DataUtil.success(myDto);
        }
        else {
            return DataUtil.errors();
        }

    }
//    @RequestMapping(value="/findAllLateStudent",method = RequestMethod.GET)
//    public DataRes findAllLateStudent(@RequestParam ClazzBean clazzBean) throws Exception{
//
//        TeacherBean teacherBean = teacherService.findAllLateStudent(clazzBean);
//        if (teacherId != null) {
//            return DataUtil.success(teacherId);
//        } else {
//            return DataUtil.errors();
//        }
//    }
    /**
     * 方法描述:
     * http://localhost:8080/teacher/update   **
     * @author: 袁海华
     * @date: 2021/12/1 19:18
     * @param: [myDto]
     * @return:
     */
    @RequestMapping(value = "update",method = RequestMethod.POST)
    public DataRes updateTeacherByUserId(@RequestBody TeacherBean  teacherBean) throws Exception{

        if(teacherBean == null){
            return null;
        }


        int counter = teacherService.updateTeacherByUserId(teacherBean);
        if(counter >0 ){
            return DataUtil.success(teacherBean);
        }else{
            return DataUtil.errors();
        }
    }
    /**
     * 方法描述:
     * @author: 袁海华
     * http://localhost:8080/teacher/list   **
     * @date: 2021/12/1 19:19 
     * @param: [pageNumber, pageSize] 
     * @return: 
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public DataRes list(@RequestParam(value="pageNumber",defaultValue = "1") Integer pageNumber,
                        @RequestParam(value="pageSize",defaultValue = "5") Integer pageSize) throws Exception{


        if(pageNumber<1){
            pageNumber=1;
        }



        Integer begin=(pageNumber-1)*pageSize;
        Integer end =pageSize;

        List<TeacherBean> teacherBeans = teacherService.findAllTeacherPage(begin,end);

        Map<String,Object> rs= new HashMap<>();
        rs.put("teacherList",teacherBeans);

        //pageNumber 第几页
        rs.put("pageNumber",pageNumber);
        //pageSize   1页多少条
        rs.put("pageSize",pageSize);

        List<TeacherBean> teacherBeanList = teacherService.findAllTeacher();
        if(!CollectionUtils.isEmpty(teacherBeanList)){
            Integer totalCounter = teacherBeanList.size();
            //totalCounter 一共多少条记录
            rs.put("totalCounter",totalCounter);

            //totalPage 共多少页
            Integer totalPage = totalCounter%pageSize==0?totalCounter/pageSize:totalCounter/pageSize+1;
            rs.put("totalPage",totalPage);

        }



        return DataUtil.success(rs);
    }
    /**
     * 方法描述:根据角色类型查看老师资料
     * http://localhost:8080/teacher/findTeacherInforByClazzType?type=0    **
     * @author: 袁海华
     * @date:  9:24
     * @param: [type]
     * @return: List<MyDto>
     */

    @RequestMapping(value="/findTeacherInforByClazzType",method = RequestMethod.GET)
    public DataRes findTeacherInforByClazzType() throws Exception{
        List<MyDto> teacherInfor = teacherService.findTeacherInforByClazzType();
        if(teacherInfor.size()>=0){
            return DataUtil.success(null);
        }
        else {
            return DataUtil.errors();
        }
    }

    /**
     * 方法描述:查看所有班级
     * http://localhost:8080/crm/teacher/findAllClazz?
     * @author: 袁海华
     * @date: 2021/11/29 9:25
     * @param: null
     * @return: clazzList
     */
//    @RequestMapping(value="/findAllClazz",method = RequestMethod.GET)
//    public DataRes findAllClazz() throws Exception{
//        List<ClazzBean> clazzList = clazzService.findAllClazz();
//        if(clazzList.size()>=0){
//            return DataUtil.success(clazzList);
//        }
//        else {
//            return DataUtil.errors();
//        }
//    }
    /**
     * 方法描述:管理员增加一个老师
     * http://localhost:8080/teacher/add?  **
     * @author: 袁海华
     * {
     *     "teacherId":111,
     *     "teacherSalary":"111",
     *     "hireDate":"2020-01-20"
     * }
     * @date: 2021/11/22 15:24
     * @param: [teacherController]
     * @return: null
     */
    @RequestMapping(value ="/add",method = RequestMethod.POST)
    public DataRes add(@RequestBody TeacherBean teacherBean)throws Exception{
        int counter=teacherService.add(teacherBean);
        if(counter==1){
            return DataUtil.success(teacherBean);
        }
        else {
            return DataUtil.errors();
        }
    }
    /**
     * 方法描述:http://localhost:8080/teacher/delete
     * @author: 袁海华
     * @date: 2021/11/23 14:25
     * @param: [teacherId]
     * @return:null
     */

    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public DataRes delete(@RequestBody Map<String,List<Integer>> map) throws Exception{

        if(CollectionUtils.isEmpty(map)||map.get("ids").isEmpty()){
            return DataUtil.errors();
        }

        List<Integer> ids = map.get("ids");


        int counter = teacherService.deleteById(ids);
        if(counter >0){
            return DataUtil.success(counter);
        }else{
            return DataUtil.errors();
        }
    }
    /**
     * 方法描述:http://localhost:8080/crm/teacher/updateById?
     * @author: 袁海华
     * @date: 2021/11/23 14:25
     * @param: [teacherBean]
     * @return:null
     */
    @RequestMapping(value = "/updateTeacherInfo",method = RequestMethod.POST)
    public  DataRes update(@RequestBody TeacherBean teacherBean)throws Exception{

        teacherService.update(teacherBean);
        int counter=teacherService.update(teacherBean);
        if(counter==1){
            return DataUtil.success(teacherBean);
        }
        else {
            return DataUtil.errors();
        }

    }
    /**
     * 方法描述:http://localhost:8080/teacher/updateTeacherInforByTeacherId?  **
     * {
     *     "id":100014,"name":"fff1","phone":"222","password":"123"
     * }
     * @author: 袁海华
     * @date: 2021/11/23 14:25
     * @param: [userBean]
     * @return:null
     */
    @RequestMapping(value = "/updateTeacherInforByTeacherId",method = RequestMethod.POST)
    public DataRes updateTeacherInforByTeacherId(@RequestBody UserBean userBean)throws Exception{

        teacherService.updateTeacherInforByTeacherId(userBean);
        int counter=teacherService.updateTeacherInforByTeacherId(userBean);
        if(counter==1){
            return DataUtil.success(userBean);

        }else {
            return DataUtil.errors();
        }
    }
    /**
     * 方法描述:http://localhost:8080/crm/teacher/findById?  **
     * @author: 袁海华
     * @date: 2021/11/23 14:25
     * @param: [teacherId]
     * @return:teacherBean
     */
    @RequestMapping(value="/findById",method = RequestMethod.GET)
    public DataRes findById(@RequestParam Integer teacherId) throws Exception{

        TeacherBean teacherBean = teacherService.findById(teacherId);
        if (teacherId != null) {
            return DataUtil.success(teacherId);
        } else {
            return DataUtil.errors();
        }
    }
    /**
     * 方法描述:http://localhost:8080/crm/teacher/findLateStudentInforByTeacherId?
     * @author: 袁海华
     * @date: 2021/11/23 14:25
     * @param: [teacherId]
     * @return:delayBeanList
     */
    @RequestMapping(value="/findLateStudentInforByTeacherId",method = RequestMethod.GET)
    public DataRes findLateStudentInforByTeacherId(@RequestParam Integer teacherId) throws Exception{
        List<DelayBean> delayBeanList = teacherService.findLateStudentInforByTeacherId(teacherId);
        if (teacherId != null) {
            return DataUtil.success(teacherId);
        } else {
        }
        return DataUtil.errors();
    }
    /**
     * 方法描述:http://localhost:8080/crm/teacher/findStudentIdByTeacherId?
     * @author: 袁海华
     * @date: 2021/11/23 14:25
     * @param: [teacherId]
     * @return:studentIdList
     */
    @RequestMapping(value="/findStudentIdByTeacherId",method = RequestMethod.GET)
    public DataRes findStudentIdByTeacherId(@RequestParam Integer teacherId)throws Exception {

        List<Integer> studentIdList = teacherService.findStudentIdByTeacherId(teacherId);
        if (teacherId != null) {
            return DataUtil.success(teacherId);
        } else {
        }
        return DataUtil.errors();
    }
    /**
     * 方法描述:根据角色Id查看所有老师
     * http://localhost:8080/crm/teacher/findAllTeacherIdByRoleId?
     * @author: 袁海华
     * @date: 2021/11/29 9:26
     * @param: [roleId]
     * @return: teacherIdList
     */
    @RequestMapping(value="/findAllTeacherIdByRoleId",method = RequestMethod.GET)
    public DataRes findAllTeacherIdByRoleId(@RequestParam Integer roleId)throws Exception {

        List<Integer> teacherIdList = teacherService.findAllTeacherIdByRoleId(roleId);
        if (roleId != null) {
            return DataUtil.success(roleId);
        } else {
        }
        return DataUtil.errors();

    }
    /**
     * 方法描述:根据老师ID增加班级
     * http://localhost:8080/crm/teacher/addClazzByTeacherId?   **
     * @author: 袁海华
     * @date: 2021/11/29 9:27
     * @param: [clazzStudentTeacherBean]
     * @return: null
     */
    @RequestMapping(value = "/addClazzByTeacherId",method = RequestMethod.POST)
    public DataRes addClazzByTeacherId(@RequestBody ClazzStudentTeacherBean clazzStudentTeacherBean)throws Exception{

        teacherService.addClazzByTeacherId(clazzStudentTeacherBean);
        int counter=teacherService.addClazzByTeacherId(clazzStudentTeacherBean);
        if(counter==1){
            return DataUtil.success(clazzStudentTeacherBean);
        }else {
            return DataUtil.errors();
        }
    }
    /**
     * 方法描述:根据ID查看老师的姓名,电话,id
     * @author: 袁海华
     * http://localhost:8080/crm/teacher/findStudentOrTeacherByNotClass?id=20020
     * @date: 2021/11/25 12:17
     * @param: [roleBean]
     * @return:teacherList
     */
//    @RequestMapping(value="/findStudentOrTeacherByNotClass",method = RequestMethod.GET)
//    public DataRes findStudentOrTeacherByNotClass(@RequestParam RoleBean roleBean) throws Exception{
//
//        List<UserBean> teacherList = teacherService.findStudentOrTeacherByNotClass(roleBean);
//        if(roleBean.getId()!=null){
//            return DataUtil.success(roleBean);
//        }else {
//            return DataUtil.errors();
//        }
//
//    }
}
